//
//  LDCRegistViewController.m
//  LaiKeDaoClient
//
//  Created by lidechen on 15/10/24.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "LDCRegistViewController.h"

@interface LDCRegistViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView             *m_tableView;
    UITextField             *mobileTF;
    UITextField             *verificationTF;
    UITextField             *passwordTF;
}

@end

@implementation LDCRegistViewController

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    [self setNaviLeftButton:@""];
    [self setNaviTitle:@"注册"];
    
    m_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 176) style:UITableViewStylePlain];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    [m_tableView setScrollEnabled:NO];
    [self.view addSubview:m_tableView];
    
}

#pragma mark - tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* str=  @"cell";
    static NSString* twostr = @"twocell";
    static NSString* thdstr = @"thdstr";
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        
        mobileTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 44)];
        mobileTF.font = [UIFont systemFontOfSize:14];
        mobileTF.placeholder = @"请输入手机号";
        [cell addSubview:mobileTF];
        
        return cell;
    }
    else if (indexPath.row == 1){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:twostr];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        
        verificationTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40-45, 44)];
        verificationTF.font = [UIFont systemFontOfSize:14];
        verificationTF.placeholder = @"验证码";
        [cell addSubview:verificationTF];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(SCREEN_WIDTH-45-20, 11, 45, 22);
        btn.backgroundColor = [UIColor lightGrayColor];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setTitle:@"获取" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(getVerificationAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:btn];
        
        return cell;
    }
    else
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:thdstr];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        }
        
        passwordTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 0, SCREEN_WIDTH-40, 44)];
        passwordTF.font = [UIFont systemFontOfSize:14];
        passwordTF.placeholder = @"密码";
        [cell addSubview:passwordTF];
        
        return cell;
    }
    return nil;
    
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 44)];
    view.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(15, 11, SCREEN_WIDTH-30, 33);
    btn.backgroundColor = [UIColor colorWithHexString:@"00b1ef"];
    btn.titleLabel.font = [UIFont systemFontOfSize:17];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitle:@"注册" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(RegistAction:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    
    return view;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 44;
}
#pragma mark-获取验证码
-(void)getVerificationAction:(UIButton*)bt{
   [CSHApiClient getTelVerifyCodeWithPhoneNum:mobileTF.text didFinishLoaded:^(KXJson *json, NSString *path) {
       
   } didFailLoaded:^(NSError *error, NSString *path) {
       
   }];
}

#pragma mark-注册
-(void)RegistAction:(id)sender{
    [CSHApiClient registerWithPhoneNum:mobileTF.text verification_code:verificationTF.text password:passwordTF.text didFinishLoaded:^(KXJson *json, NSString *path) {
        if ([json getIntForKey:@"code"]==200) {
            KXJson *json1=[json getJsonForKey:@"data"];
            [[UserInfo SharedUserfo] setUser_token:[json1 getStringForKey:@"user_token"]];
            SharedAppDelegate.window.rootViewController = SharedAppDelegate.tabBarController;
        }
      
    } didFailLoaded:^(NSError *error, NSString *path) {
    }];
    
}
-(void)cancleKeyboard{
    [mobileTF resignFirstResponder];
    [verificationTF resignFirstResponder];
    [passwordTF resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
